package com.neuedu.servlet;


import com.neuedu.dao.DeptDao;
import com.neuedu.entity.Dept;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * http://localhost:8080/web/get
 */
@WebServlet(urlPatterns = "/get")
public class OutServlet  extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //响应内容
        response.setContentType("text/html;charset=utf8");

        String html = genHtml();

//        ServletOutputStream outputStream = response.getOutputStream();
        PrintWriter out = response.getWriter();
        out.write(html);
        out.flush();
        out.close();

    }



    private String genHtml(){

        DeptDao deptDao = new DeptDao();


        String where  = "  ";
        List<Dept> depts = deptDao.selectList(where);



        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("    <table border=\"1\" width=\"500px\"  cellpadding=\"0\" cellspacing=\"0\">");
        stringBuilder.append("    	<caption>Java+安卓1班名单</caption>                                    ");
        stringBuilder.append("    	<tr align=\"center\">                                                  ");
        stringBuilder.append("    		<td>部门id</td>                                                      ");
        stringBuilder.append("    		<td>部门名称</td>                                                      ");
        stringBuilder.append("    		<td>办公地址</td>                                                      ");

        for (int i = 0; i < depts.size(); i++) {
            Dept dept = depts.get(i);
            stringBuilder.append("    	</tr>                                                                  ");
            stringBuilder.append("    	<tr align=\"center\">                                                  ");
            stringBuilder.append("    		<td>"+dept.getDeptno()+"</td>                                                         ");
            stringBuilder.append("    		<td>"+dept.getDname()+"</td>                                                         ");
            stringBuilder.append("    		<td>"+dept.getLoc()+"</td>                                                         ");
            stringBuilder.append("    	</tr>                                                                  ");
        }




        stringBuilder.append("    </table>                                                                 ");

        return stringBuilder.toString();
    }
}
